home *** CD-ROM | disk | FTP | other *** search
- #include "lib.h"
- /* sleep(3)
- *
- * Sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt.
- *
- * Changed to conform with POSIX Terrence W. Holm Oct. 1988
- */
-
- #include <signal.h>
-
-
- static void _alfun() /* Used with sleep() below */
- {
- }
-
-
- unsigned sleep( secs )
- unsigned secs;
-
- {
- unsigned current_secs;
- unsigned remaining_secs;
- #ifdef __STDC__
- void (*old_signal)();
- #else
- int (*old_signal)();
- #endif
-
- if ( secs == 0 )
- return( 0 );
-
- current_secs = alarm( 0 ); /* Is there currently an alarm? */
-
- if ( current_secs == 0 || current_secs > secs )
- {
- old_signal = signal( SIGALRM, _alfun );
-
- alarm( secs );
- pause();
- remaining_secs = alarm( 0 );
-
- signal( SIGALRM, old_signal );
-
- if ( current_secs > secs )
- alarm( current_secs - ( secs - remaining_secs ) );
-
- return( remaining_secs );
- }
-
-
- /* current_secs <= secs, ie. alarm should occur before secs */
-
- alarm( current_secs );
- pause();
- remaining_secs = alarm( 0 );
-
- alarm( remaining_secs );
-
- return( secs - ( current_secs - remaining_secs ) );
- }
-